package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditPageQueryResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description WorkerCertificationAuditServiceImpl
 * @Author dmx
 * @Date 2024-12-03
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements WorkerCertificationAuditService {

    @Override
    public WorkerCertificationAuditPageQueryResDTO getPage(WorkerCertificationAuditPageQueryReqDTO dto) {
        IPage<WorkerCertificationAudit> page=new Page<>(dto.getPageNo(),dto.getPageSize());
        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery();

//        if (dto.getAuditStatus()!=null){
//            wrapper.eq(WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus());
//        }
        if (dto.getAuditStatus()!=null){
            wrapper.eq(WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus());
        }
        if (dto.getIdCardNo()!=null){
            wrapper.likeRight(WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo());
        }
        if (dto.getCertificationStatus()!=null){
            wrapper.eq(WorkerCertificationAudit::getCertificationStatus,dto.getCertificationStatus());
        }
//        if (dto.getAuditStatus()!=null){
//            wrapper.eq(WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus());
//        }
        if (dto.getName()!=null){
            wrapper.like(WorkerCertificationAudit::getName, dto.getName());
        }
        //wrapper.like(WorkerCertificationAudit::getName, dto.getName());
        this.page(page,wrapper);

        WorkerCertificationAuditPageQueryResDTO resDTO = new WorkerCertificationAuditPageQueryResDTO();
        resDTO.setList(page.getRecords());
        resDTO.setPages(page.getPages());
        resDTO.setTotal(page.getTotal());
        return resDTO;
    }


    /**
     * 审核服务人员认证信息
     * @param id
     * @param certificationStatus
     * @param rejectReason
     */
    @Override
    public void audit(Long id, Integer certificationStatus, String rejectReason) {


        //1.判断对应id的服务人员认证数据是否为空
        WorkerCertificationAudit workerCertificationAudit = this.getById(id);
        if (BeanUtils.isEmpty(workerCertificationAudit)){
            throw new BadRequestException("认证信息不存在，认证失败");
        }
        //2.修改对应id的人员认证数据的审核状态和认证状态
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        boolean update = this.lambdaUpdate()
                .eq(WorkerCertificationAudit::getId, id)
                .set(WorkerCertificationAudit::getAuditStatus, CertificationStatusEnum.PROGRESSING.getStatus())
                .set(WorkerCertificationAudit::getCertificationStatus, certificationStatus)
                .set(WorkerCertificationAudit::getRejectReason, rejectReason)
                .set(WorkerCertificationAudit::getAuditorName, currentUserInfo.getName())
                .set(WorkerCertificationAudit::getAuditTime, LocalDateTime.now())
                .set(WorkerCertificationAudit::getAuditorId, currentUserInfo.getId()).update();

        //3.判断是否修改成功
        if(!update){
            throw new DBException("修改认证失败");
        }
    }
}
